package com.dianban.service.impl;


import com.dianban.dao.FacePayStoreOrderDao;
import com.dianban.entity.FacePayStoreOrder;
import com.dianban.service.FacePayCommissionLogService;
import com.dianban.service.FacePayStoreOrderService;
import com.dianban.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户业务实现层
 *
 * Created by bysocket on 07/02/2017.
 */
@Service
@Transactional(readOnly = true)
public class FacePayStoreOrderServiceImpl implements FacePayStoreOrderService {

    @Autowired
    private FacePayStoreOrderDao facePayStoreOrderDao;
    @Autowired
    private FacePayCommissionLogService facePayCommissionLogService;

    @Override
    public FacePayStoreOrder findById(Long id) {
        return facePayStoreOrderDao.findById(id);
    }

    @Override
    public FacePayStoreOrder findByOrderNumber(String orderNumber){
        return facePayStoreOrderDao.findByOrderNumber(orderNumber);
    }

    @Override
    public FacePayStoreOrder findByPayNumber(String payNumber){
        return facePayStoreOrderDao.findByPayNumber(payNumber);
    }

    @Override
    public List<FacePayStoreOrder > findVerifyList(Long  userId, Long storeId, String money,Integer payStatus,String payType){
        return facePayStoreOrderDao.findVerifyList(userId,storeId,money,payStatus,payType);
    }

    @Override
    @Transactional(readOnly = false)
    public int insert(FacePayStoreOrder facePayStoreOrder) {
        facePayStoreOrder.setCreatetime(System.currentTimeMillis()/1000);
        return facePayStoreOrderDao.insert(facePayStoreOrder);
    }

    @Override
    public String buildOrderNumber(){
        String orderNumber = "OR"+DateUtils.format(new Date(),DateUtils.YMDHMSS);
        return orderNumber;
    }

    @Override
    public String buildOrderNumber(String prefix){
        String orderNumber = prefix+DateUtils.format(new Date(),DateUtils.YMDHMSS);
        return orderNumber;
    }


    @Override
    @Transactional(readOnly = false)
    public int updateState(Long id, Integer payStatus) {
        return facePayStoreOrderDao.updateState(id,payStatus,System.currentTimeMillis()/1000);
    }

    @Override
    @Transactional(readOnly = false)
    public int updateVerify(Long id, Integer hasVerifyCount, Integer takenewHasVerifyCount) {
        return facePayStoreOrderDao.updateVerify(id,hasVerifyCount,takenewHasVerifyCount,System.currentTimeMillis()/1000);
    }

    @Override
    @Transactional(readOnly = false)
    public int updateCommissionStatus(Long id, Integer commissionStatus) {
        return facePayStoreOrderDao.updateCommissionStatus(id,commissionStatus,System.currentTimeMillis()/1000);
    }

    @Override
    @Transactional(readOnly = false)
    public int  updateStateAndCommission(FacePayStoreOrder facePayStoreOrder){
        int count = updateState(facePayStoreOrder.getId(), 20);
        if (count == 1) {
            facePayStoreOrder.setPayStatus(20);
            // 线下订单分润
            facePayCommissionLogService.commission(facePayStoreOrder);
        }
        return count;
    }

    @Override
    @Transactional(readOnly = false)
    public void updatePayTypeAndChannel(Long id, String payTypeAndChannel) {
        facePayStoreOrderDao.updatePayTypeAndChannel(id, payTypeAndChannel);
    }

    @Override
    @Transactional(readOnly = false)
    public int updateSlj(FacePayStoreOrder facePayStoreOrder) {
        facePayStoreOrder.setUpdatetime(System.currentTimeMillis()/1000);
        return facePayStoreOrderDao.updateSlj(facePayStoreOrder);
    }

    @Override
    public List<FacePayStoreOrder> findByParams(String monthDayBegin, String monthDayEnd, Long userId, Long storeId, String payType, String payStatus, String money) {
        return facePayStoreOrderDao.findByParams(monthDayBegin, monthDayEnd, userId, storeId, payType, payStatus, money);
    }

    @Override
    public List<Map<String, Object>> statistics(List<String> storeIdList) {
        return facePayStoreOrderDao.statistics(storeIdList);
    }

    @Override
    @Transactional(readOnly = false)
    public void update(FacePayStoreOrder facePayStoreOrder) {
        facePayStoreOrderDao.update(facePayStoreOrder);
    }

}
